package tech.cn.note.word.fun;

import java.math.BigDecimal;

import com.deepoove.poi.render.compute.RenderDataCompute;
import org.apache.commons.lang3.StringUtils;
import tech.cn.note.utils.BigDecimalUtil;

import org.springframework.stereotype.Service;

import static org.springframework.util.ObjectUtils.isEmpty;

@Service
public class ThdNum implements RenderFunction {
    /**
     * @param fields            参数1 金额
     * @param renderDataCompute
     * @return
     */
    @Override
    public String doCalculate(String[] fields, RenderDataCompute renderDataCompute) {
        if (isEmpty(fields)) {
            return "";
        }
        String placeHolder = fields[0];
        Object data = renderDataCompute.compute(placeHolder);
        BigDecimal numValue = BigDecimalUtil.parse(data);
        if (numValue == null) {
            return "";
        }

        int pointNum;
        if (fields.length > 1 && fields[1] != null
                && StringUtils.isNotBlank(fields[1])) {
            pointNum = Integer.parseInt(fields[1]);
        } else {
            pointNum = numValue.scale();
        }
        return BigDecimalUtil.formatThd(numValue, pointNum);
    }
}
